import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

/// 访问相册需要权限声明
/// iOS中申请诸如：Privacy - Photo Library Usage Description、Privacy - Microphone Usage Description、Privacy - Camera Usage Description
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '图片选择',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        splashColor: Colors.transparent,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('图片选择'),
        ),
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  File _imageFile;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          ElevatedButton(
            onPressed: _pickImage,
            child: Text('选择图片'),
          ),
          _imageFile == null ? Text('未选择图片') : Image.file(_imageFile),
        ],
      ),
    );
  }

  void _pickImage() async {
    PickedFile file = await ImagePicker().getImage(source: ImageSource.gallery);
    setState(() {
      _imageFile = File(file.path);
    });
  }
}
